$(function () {
    var paymentHandle_datagrid, paymentHandle_btn, payment_dialog, payment_form, search_form,payment_check_form,payment_check_dialog;
    payment_dialog = $("#payment_dialog");
    payment_check_dialog = $("#payment_check_dialog");
    payment_check_form = $("#payment_check_form");
    search_form = $("#search_form");
    paymentHandle_datagrid = $("#paymentHandle_datagrid");
    paymentHandle_btn = $("#paymentHandle_btn");
    payment_form = $("#payment_form");
    //页面的展示
    paymentHandle_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/paymentHandle/list",
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        columns: [[
            {field: 'orderBill', title: '申请单号', width: 10, align: 'center', formatter: orderBillNumberFormatter},
            {field: 'client', title: '投保人', width: 10, align: 'center', formatter: clientFormatter},
            {field: 'paymentType', title: '缴费类型', width: 10, align: 'center', formatter: paymentTypeFormatter},
            {field: 'product', title: '产品', width: 10, align: 'center', formatter: productNameFormatter},
            {field: 'organization', title: '承保机构', width: 10, align: 'center', formatter: organizationFormatter},
            {field: 'beginTime', title: '起保日期', width: 10, align: 'center', formatter: beginTimeFormatter},
            {field: 'endTime', title: '停保日期', width: 10, align: 'center', formatter: endTimeFormatter},
            {field: 'issue', title: '期次', width: 10, align: 'center', formatter: issuetFormatter},
            {field: 'amount', title: '保费金额', width: 10, align: 'center', formatter: amountFormatter},
            {field: 'coverage', title: '应缴金额', width: 10, align: 'center', formatter: coverageFormatter},
            {field: 'state', title: '处理状态', width: 10, align: 'center', formatter: stateFormatter},
            {field: 'serialNumber', title: '收据流水号', width: 10, align: 'center'}
        ]],
        onClickRow: function (index, row) {
            if (row.orderBill.state == 21) {
                $("#pay").linkbutton("enable");
                $("#rollback").linkbutton("enable");
            } else if (row.orderBill.state == 22) {
                $("#pay").linkbutton("disable");
                $("#rollback").linkbutton("enable");
            } else {
                $("#pay").linkbutton("disable");
                $("#rollback").linkbutton("disable");
            }
        }
    });
    //设置文本框的值
    $("#input_state").textbox("setText",'111');
    //
    //设置下拉列表框不能让用户自定义内容
    $(".easyui-combobox").combobox({"editable": false});
    // 统一管理点击事件
    $("a[data-cmd]").on("click", function () {
        var cmd = $(this).data("cmd");
        if (cmd) {
            cmdObj[cmd]();
        }
    });
    //统一处理点击事件
    var cmdObj = {
        //点开缴费页面
        pay: function () {
            var rowData = paymentHandle_datagrid.datagrid("getSelected");
            console.log(rowData)
            //自定义orderBillId,便于paymentConfirm使用

            //---------------------------------------------------------------------------------------
            if (rowData) {
                $("#orderBillId").val(rowData.orderBill.id);
                console.log($("#orderBillId").val())
                if (rowData.orderBill.state == 21) {
                    rowData['client.realName'] = rowData.client.realName;
                    rowData['orderBill.orderBillNumber'] = rowData.orderBill.orderBillNumber;
                    rowData['product.name'] = rowData.product.name;
                    rowData['orderBill.beginTime'] = rowData.orderBill.beginTime;
                    rowData['orderBill.endTime'] = rowData.orderBill.endTime;
                    rowData['orderBill.issue'] = rowData.orderBill.issue;
                    rowData['premium.amount'] = rowData.premium.amount;
                    rowData['premium.coverage'] = rowData.premium.coverage;
                    rowData['organization.organizationName'] = rowData.organization.organizationName;
                    rowData['auditInsurance.pushTime'] = rowData.auditInsurance.pushTime;
                    payment_dialog.dialog("open");
                    $("#payment_form :input").prop("readOnly", true);
                    payment_form.form("load", rowData);
                } else {
                    $.messager.alert("温馨提示", "核保未通过,无法进行缴费!", "error");
                }
            } else {
                $.messager.alert("温馨提示", "请选一条数据进行缴费", "info");
            }
        },
        //
        //关键字搜索
        search: function () {
            var everyNo = $("[name=everyNo]").val();
            var issue = $("[name=issue]").val();
            var amount = $("[name=amount]").val();
            var clientName = $("[name=clientName]").val();
            var paymentType = $("[name=paymentType]").val();
            var productId = $("[name=productId]").val();
            var state = $("[name=state]").val();
            var organizationId = $("[name=organizationId]").val();
            var minDate = $("[name=minDate]").val();
            var maxDate = $("[name=maxDate]").val();
            if (minDate == "") {
                minDate = '01/01/1949'
            }
            if (maxDate == "") {
                maxDate = '12/31/2099'
            }
            paymentHandle_datagrid.datagrid("load", {
                everyNo: everyNo,
                issue: issue,
                amount: amount,
                clientName: clientName,
                paymentType: paymentType,
                productId: productId,
                state: state,
                organizationId: organizationId,
                minDate: minDate,
                maxDate: maxDate
            })
        },
        //
        //重置--清空高级查询数据
        reset: function () {
            search_form.form("reset");
            var everyNo = $("[name=everyNo]").val();
            var issue = $("[name=issue]").val();
            var amount = $("[name=amount]").val();
            var clientName = $("[name=clientName]").val();
            var paymentType = $("[name=paymentType]").val();
            var productId = $("[name=productId]").val();
            var state = $("[name=state]").val();
            var organizationId = $("[name=organizationId]").val();
            var minDate = $("[name=minDate]").val();
            var maxDate = $("[name=maxDate]").val();
            if (minDate == "") {
                minDate = '01/01/1949'
            }
            if (maxDate == "") {
                maxDate = '12/31/2099'
            }
            paymentHandle_datagrid.datagrid("load", {
                everyNo: everyNo,
                issue: issue,
                amount: amount,
                clientName: clientName,
                paymentType: paymentType,
                productId: productId,
                state: state,
                organizationId: organizationId,
                minDate: minDate,
                maxDate: maxDate
            })
        },
        //打印
        print: function () {
            var rowData = paymentHandle_datagrid.datagrid("getSelected");
            if (rowData) {
                $.messager.confirm("温馨提示", "确定打印这条保单的缴费通知书吗?", function (yes) {
                    if (yes) {
                        $.messager.alert("温馨提示", "正在打印...请稍后...", "info")
                    }
                })
            } else {
                $.messager.alert("温馨提示", "请选一条数据进行打印!", "info");
            }
        },
        //见费出单退回
        rollback: function () {
            $.messager.confirm("温馨提示", "确认退回此张保单吗?", function (yes) {
                if (yes) {
                    $.messager.alert("温馨提示", "退单成功", "info")
                }
            })
        },
        //刷新页面
        reloadPage:function () {
            window.location.href="/paymentHandle";
        },
        //
        //查看缴费信息
        check: function () {
            var rowData = paymentHandle_datagrid.datagrid("getSelected");
            /*自定义orderBillId,便于paymentConfirm使用
            $("#orderBillId").val(rowData.orderBill.id);*/
            if (rowData) {
                    rowData['client.realName'] = rowData.client.realName;
                    rowData['orderBill.orderBillNumber'] = rowData.orderBill.orderBillNumber;
                    rowData['product.name'] = rowData.product.name;
                    rowData['orderBill.beginTime'] = rowData.orderBill.beginTime;
                    rowData['orderBill.endTime'] = rowData.orderBill.endTime;
                    rowData['orderBill.issue'] = rowData.orderBill.issue;
                    rowData['premium.amount'] = rowData.premium.amount;
                    rowData['premium.coverage'] = rowData.premium.coverage;
                    rowData['organization.organizationName'] = rowData.organization.organizationName;
                    rowData['auditInsurance.pushTime'] = rowData.auditInsurance.pushTime;
                    rowData['orderBill.state'] = rowData.orderBill.state;
                    payment_check_dialog.dialog("open");
                    if (rowData.orderBill.state == 21){
                        $("#input_state").textbox("setText",'核保成功');
                    }else if(rowData.orderBill.state == 22){
                        $("#input_state").textbox("setText",'核保失败');
                    }else if(rowData.orderBill.state == 31){
                        $("#input_state").textbox("setText",'缴费成功');
                    }else if(rowData.orderBill.state == 32){
                        $("#input_state").textbox("setText",'缴费失败');
                    }
                    $("#payment_form :input").prop("readOnly", true);
                    payment_check_form.form("load", rowData);
            } else {
                $.messager.alert("温馨提示", "请选择一条需要查看的数据", "info");
            }
        }
        //
    }
    //
});

function orderBillNumberFormatter(value, row, index) {
    return row.orderBill.orderBillNumber;
}

function issuetFormatter(value, row, index) {
    return row.orderBill.issue;
}

function productNameFormatter(value, row, index) {
    return row.product.name;
}

function paymentTypeFormatter(value, row, index) {
    return row.orderBill.paymentType == 1 ? "<font>现金缴费</font>" : "<font>刷卡缴费</font>";
}

function beginTimeFormatter(value, row, index) {
    return row.orderBill.beginTime;
}

function endTimeFormatter(value, row, index) {
    return row.orderBill.endTime;
}

function stateFormatter(value, row, index) {
    if (row.orderBill.state == 21) {
        return "<font>核保成功</font>";
    } else if (row.orderBill.state == 22) {
        return "<font color='red'>核保失败</font>";
    } else if (row.orderBill.state == 31) {
        return "<font>缴费成功</font>";
    } else  if (row.orderBill.state == 32) {
        return "<font color='red'>缴费失败</font>";
    }else  if (row.orderBill.state == 41) {
        return "<font color='blue'>续保保单</font>";
    }
}

function amountFormatter(value, row, index) {
    return row.premium.amount;
}

function coverageFormatter(value, row, index) {
    return row.premium.coverage;
}

function clientFormatter(value, row, index) {
    return row.client.realName;
}

function organizationFormatter(value, row, index) {
    return row.organization.organizationName;
}

